package com.yuantu.judiciary.controller;

import com.yuantu.judiciary.common.Response;
import com.yuantu.judiciary.common.ResponseFactory;
import com.yuantu.judiciary.model.enums.MessageType;
import com.yuantu.judiciary.model.vo.MessageInfoVO;
import com.yuantu.judiciary.model.vo.MessageNumVO;
import com.yuantu.judiciary.model.vo.PageVO;
import com.yuantu.judiciary.service.IMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息管理模块
 *
 * @author syw
 * @since 2023-08-23
 **/
@RestController
@RequestMapping(value = "/message")
@Api(tags = "消息管理模块")
@RequiredArgsConstructor
public class MessageController extends BaseController {

    private final IMessageService messageService;

    @ApiOperation(value = "小程序-获取消息个数")
    @GetMapping("/get/count")
    public Response<Integer> getMessageCount() {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(messageService.getMessageCount(accountId));
    }

    @ApiOperation(value = "小程序-获取消息数量")
    @GetMapping("/get/num")
    public Response<List<MessageNumVO>> getMessageNum() {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(messageService.getMessageNum(accountId));
    }

    @ApiOperation(value = "小程序-根据类型获取消息")
    @GetMapping("/get/list/by-type")
    public Response<PageVO<MessageInfoVO>> getListByType(@PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable,
                                                         @RequestParam MessageType type) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(messageService.getListByType(accountId, type, pageable));
    }

    @ApiOperation(value = "小程序-动态消息已读")
    @PostMapping("/read")
    public Response<Boolean> read(@RequestParam Long id) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(messageService.read(accountId, id));
    }

    @ApiOperation(value = "小程序-动态消息全部已读")
    @PostMapping("/read/all")
    public Response<Boolean> readAll() {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(messageService.readAllByType(accountId, MessageType.DYNAMIC));
    }

}
